package com.hmall.trade.config;

import com.hmall.common.utils.UserContext;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Feign拦截器（负责从ThreadLocal中获取用户信息设置到请求头）
 *
 * @Author mr.lcc
 * @Data 2025/6/20 16:03
 */
@Configuration
@Slf4j
public class UserInfoRequestInterceptorConfig {

    /**
     * 有状态的调用，获取到用户id
     *
     * @return
     */
    @Bean
    public RequestInterceptor userInfoRequestInterceptor() {
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate requestTemplate) {
                Long userId = UserContext.getUser();
                if (userId != null) {
                    log.info("[UserInfoRequestInterceptorConfig]获取到用户的Id:{},", userId);
                    requestTemplate.header("user-info", userId.toString());
                }
            }
        };
    }
}
